﻿using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows.Input;
using System.Xml.Serialization;
using MaternityHospitalPatients.InputContract;
using MaternityHospitalPatients.Services;
using Microsoft.Win32;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using ViewModelBase = Telerik.Windows.Controls.ViewModelBase;

namespace MaternityHospitalPatients.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        private readonly ImportService _importService;
        private bool _isBusy;
        private int _progress;

        /// <summary>
        /// Initializes a new instance of the MvvmViewModel1 class.
        /// </summary>
        public MainViewModel()
        {
            _importService = new ImportService();
        }

        #region Properties

        public QueryableCollectionView Data
        {
            get { return new QueryableCollectionView(_importService.GetPatientRecord()); }
        }

        public QueryableCollectionView Debtors
        {
            get { return new QueryableCollectionView(_importService.GetDebtors()); }
        }

        public int Progress
        {
            get { return _progress; }
            set
            {
                _progress = value;
                OnPropertyChanged(() => Progress);
            }
        }

        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                OnPropertyChanged(() => IsBusy);
            }
        }

        #endregion

        #region Commands

        public ICommand OnImportClick
        {
            get { return new DelegateCommand(LoadFile); }
        }

        #endregion

        private void LoadFile(object obj)
        {
            var openFileDialog = new OpenFileDialog {Filter = "Xml|*.xml"};
            if (openFileDialog.ShowDialog() == true)
            {
                Report report;
                using (
                    var streamReader = new StreamReader(openFileDialog.FileName, Encoding.GetEncoding("windows-1251")))
                {
                    var xmlSerializer = new XmlSerializer(typeof (Report));
                    report = (Report) xmlSerializer.Deserialize(streamReader);
                }
                var backgroundWorker = new BackgroundWorker {WorkerReportsProgress = true};
                backgroundWorker.ProgressChanged += BackgroundWorkerOnProgressChanged;
                backgroundWorker.DoWork += (sender, args) =>
                                               {
                                                   IsBusy = true;
                                                   _importService.Import(report, backgroundWorker.ReportProgress);
                                               };
                backgroundWorker.RunWorkerCompleted += (sender, args) =>
                                                           {
                                                               IsBusy = false;
                                                               OnPropertyChanged(() => Progress);
                                                           };
                backgroundWorker.RunWorkerAsync();
            }
        }

        private void BackgroundWorkerOnProgressChanged(object sender, ProgressChangedEventArgs progressChangedEventArgs)
        {
            Progress = progressChangedEventArgs.ProgressPercentage;
        }
    }
}